package ch.exploadingelephants.graph;

import java.util.List;

import ch.exploadingelephants.model.ConveyorDirection;

/**
 * @author Sandro De Zanet
 * 
 */
public class WriterToken extends Token {

    private DotColor color;
    private Token child;
    private ConveyorDirection direction;

    /**
     * 
     */
    public WriterToken() {
    }

    /**
     * @param color
     */
    public WriterToken(DotColor color, ConveyorDirection direction) {
	this.color = color;
	this.direction = direction;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#accept(ch.exploadingelephants.graph
     * .DotString)
     */
    @Override
    public TokenResult accept(DotString inputPattern,
	    List<VisitedNodeState> visited) throws InfinityLoopException {
	checkForInfiniteLoop(inputPattern, visited);
	DotString newString = inputPattern.copy();
	newString.append(color);
	return child.accept(newString, visited);
    }

    /**
     * @param end
     */
    public void connect(Token child) {
	this.child = child;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * ch.exploadingelephants.graph.Token#tryConnect(ch.exploadingelephants.
     * graph.Token, ch.exploadingelephants.model.ConveyorDirection)
     */
    @Override
    public boolean tryConnect(Token createGraphObject,
	    ConveyorDirection direction) {
	if (this.direction.equals(direction)) {
	    connect(createGraphObject);
	    return true;
	}
	return false;
    }

}
